let guanliname=glqx('读者管理')


function xuanran(data){
$.each(data,(index,item)=>{
    $('#mybody').append(`
    <tr>
      <td>${item.name}</td>
      <td>${item.jiaose}</td>
      <td class='zhnegjian'>${item.shenfen}</td>
      <td>${item.yajing}</td>
      <td>${item.qianfei}</td>
      <td>${item.kahao}</td>
      <td>${item.xingbie}</td>
      <td>${item.kaishiriqi}</td>
      <td>${item.jiezhiriqi}</td>
      <td>${item.zhuangtai}</td>
     <td>
           <Button type=""data-shuju = ${JSON.stringify(item)} data-xiabiao =${index} class ='xiugai'>修改</Button>
           <Button type="" data-shenfen=${item.shenfen} id='shanchu'>删除</Button>
      </td>
 </tr> 
 `
 )
})

}
let zuidayema
var gengaihou = myshuju
let zuihou = []
let ye=1
let m


function fenye( num,data){
for(i=0;i< $('#yema .xiaobiaoye').length;i++){
  $('#yema .xiaobiaoye').eq(i).css("background-color","white")}
$('#mybody').html('')
$('#yema .xiaobiaoye').eq(num-1).css("background-color","red")
let qishi = (num-1)*10
let fenyehou = data.slice(qishi,qishi+10)

xuanran(fenyehou)
if(fenyehou.length<16){
  for(let i=0;i<=16-fenyehou.length;i++){
      $('#mybody').append(`
      <tr>
     <td></td>
     <td></td>
     <td></td>
     <td></td>
     <td></td>
     <td></td>
     <td></td>
     <td></td>
     <td></td>
     <td></td>
     <td></td>
     
     
     
  </tr>`)
  }
}
ye=num

}


function xuanzheyema(data){
$('#yema').html('')
  zuidayema = Math.ceil(data.length/10)
// for(let i=1;i<=zuidayema;i++){        
//  
  // if(zuidayema>=10){
      // if(i<=3){
          // $('#yema').append(`<div class="xiaobiaoye">${i}</div>`)  
      // }
      // if(i>zuidayema-3){
      //  $('#yema').append(`<div class="xiaobiaoye">${i}</div>`) 
      //  }
      // if(i>3&&i<=4){
          // $('#yema').append(`<div class="">...</div>`)
        //  
      // }
    //  
    //  
    //  
      //  
  // }else
      // $('#yema').append(`<div class="xiaobiaoye">${i}</div>`) 
  // }
  
   

 if(zuidayema){
  for(let i=1;i<=zuidayema;i++){
      $('#yema').append(`<div class="xiaobiaoye">${i}</div>`)
  } 
 }else{
   for(let i=1;i<=3;i++){
    $('#yema').append(`<div class="xiaobiaoye">${i}</div>`)
   }
   $('#yema').append(`<div class="xiaobiaoye">...</div>`)
   for(let i=1;i<=zuidayema-3;i++){
    $('#yema').append(`<div class="xiaobiaoye">${i}</div>`)
   }
  } 
 }           




$('#yema').on('click','.xiaobiaoye',function(){

num =parseInt($(this).text())
ye = num
// $(this).css("background-color","red")
fenye(num,gengaihou)

})

//在上一页之前添加一个显示页面数量的代码
$('#shangyiye').before('<div class="yemaxianshi">10/页</div>')
//下一页功能
$('#xiayiye').click(function(){
ye=ye+1
if(ye<=zuidayema){
fenye(ye,gengaihou)
}
})
//上一页功能
$('#shangyiye').click(function(){
ye=ye-1
if(ye>=1){
fenye(ye, gengaihou)
}
})
//跳转功能
$('.tiaozhuan').click(function(){

let tiaozhuan=  parseInt( $('#tiaozhuanye').val())
fenye(tiaozhuan,gengaihou)

})
//删除功能
$('#mybody').on('click','#shanchu',function(){
  if(czqx(guanliname,'删除')){
    return
  }
let shenfen =  $(this).attr('data-shenfen')

$('#zbc').css('display','block')
$('.anniu').on('click','.btn',function(){//托管式添加事件

if($(this).text()=='确认'){
for(let i=0;i<gengaihou.length;i++){
if(gengaihou[i].shenfen==shenfen){
  gengaihou.splice(i,1)
  $('#zbc').css('display','none')
  xuanran(gengaihou)

  xuanzheyema(gengaihou)
  fenye(1,gengaihou)
  }
}


}else{
$('#zbc').css('display','none') 
}
})

})

//查询功能
$('.chaxun').click(function(){
  if(czqx(guanliname,'查询')){
    return
  }
let xinming = $('.xinming').val()
let kahao = $('.kahao').val()
let shenfen = $('.shenfen').val()
gengaihou=myshuju//重复查询让gengaigou成为一个变量
if(xinming!=''){
gengaihou=gengaihou.filter(item=>item.name==xinming)
}
if(kahao!=''){
gengaihou=gengaihou.filter(item=>item.kahao==kahao)
}
if(shenfen!=''){
gengaihou=gengaihou.filter(item=>item.shenfen==shenfen)
}
xuanzheyema(gengaihou)//渲染初始页面
fenye(1,gengaihou)
console.log(gengaihou)

})
xuanzheyema(gengaihou)//渲染初始页面
fenye(1,gengaihou)
//修改读取数据功能
let shuju
let xiabiao 
$('#mybody').on('click','.xiugai',function(){
  if(czqx(guanliname,'编辑')){
    return
  }

$('#bianji').css('display','block')
// $('.in').attr('checked','checked' )
// console.log(JSON.parse($(this).parent().parent().children().text()))
shuju =JSON.parse($(this).attr('data-shuju'))
xiabiao =JSON.parse($(this).attr('data-xiabiao'))
$('.mingzishuru').val( shuju.name)
$('.kahaoshuru').val( shuju.kahao)
$('.banzhengriqi').val(shuju.kaishiriqi)
$('.jiezhiriqi').val(shuju.jiezhiriqi)
let n = shuju.shenfen 
var str = n.toString()
let array= str.split("")
let nianfen=array.slice(6,10)
let yuefen = array.slice(10,12)
let riqi = array.slice(12,14)
let mn = nianfen.join('')
let my = yuefen.join('')
let mr = riqi.join('')
shengri=mn+'-'+my+'-'+mr
$('.shengri').val(shengri)



for(let i=0;i<$('.huiyuan div').length;i++){
if($('.huiyuan div').eq(i).text()==shuju.jiaose){
$('.huiyuan div').children().prop('checked',false)
$('.huiyuan div').eq(i).children().prop('checked',true)//用户状态的获取
}
}
for(let k=0;k<('.xin div').length;k++){
if($('.xin div').eq(k).text()==shuju.xingbie){
$('.xin div').children().prop('checked',false)
$('.xin div').eq(k).children().prop('checked',true)//用户状态的获取 
}
}






$('.quxiao').click(function(){
$('#bianji').css('display','none')
xuanzheyema( gengaihou)
fenye(1, gengaihou)
})
})
$('.duka').click(function(){
gengaihou=myshuju
xuanzheyema(gengaihou)
fenye(1,gengaihou)  
})
$('.tijiao').click(function(){//给模态框的确认键绑定点击事件
console.log(123)
// 
// for(let i=0;i<gengaihou.length;i++){
// if(gengaihou[i].shenfen==shuju.shenfen){
  gengaihou[xiabiao].name=$('.mingzishuru').val()
  gengaihou[xiabiao].kahao=$('.kahaoshuru').val()
for(let k=0;k<$('.huiyuan div').length;k++){
 if($('.huiyuan div').eq(k).children().prop('checked')){
  gengaihou[xiabiao].jiaose =$('.huiyuan div').eq(k).text()
     break
 }
}
for(let k=0;k<$('.xin div').length;k++){
if($('.xin div').eq(k).children().prop('checked')){
  gengaihou[xiabiao].xingbie =$('.xin div').eq(k).text()
  break
  }
}
// gengaihou=myshuju
$('#bianji').css('display','none')
console.log(gengaihou)
xuanzheyema( gengaihou)
fenye(1, gengaihou)
    
// }

//    } 
})